const utils = require("../../../utils/utils");

// pages/my/feedback/feedback.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    choose: 0
  },
  chooseAddress(e) {
   if(this.data.choose==0){
      return false;
    }
    var index = e.currentTarget.dataset.index;
    var id = this.data.list[index].id;
    var trueName = this.data.list[index].realName;
    var mobile = this.data.list[index].tel;
    var address = this.data.list[index].address;
    var houseNumber = this.data.list[index].houseNumber;
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];
    
    prevPage.setData({
        ['addressInfo.id']:id,
        ['addressInfo.trueName']:trueName,
        ['addressInfo.mobile']:mobile,
        ['addressInfo.address']:address+houseNumber,
        ['addressInfo.change']:1
    })
    wx.navigateBack()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    if (utils.is_define(options.choose)) {
      that.setData({
        choose: options.choose
      })
    }
    that.getIndex()
    wx.getSystemInfo({
      success: (result) => {
        var model = result.model;
        if (model == 'iPhone X') {
          that.setData({
            pdbottom: '108',
          })
        } else {
          that.setData({
            pdbottom: '68'
          })
        }
      },
    })
  },
  goEdit(e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/my/addAdress/addAdress?id=' + id,
    })
  },
  changeDefault(e) {
    var that = this;

    var index = e.currentTarget.dataset.index;
    var name = that.data.list[index].realName;
    var mobile = that.data.list[index].tel;
    var address = that.data.list[index].address;
    var isDefault = that.data.list[index].defaultAddress == 1 ? '-1' : 1;
    var lon = that.data.list[index].lng;
    var lat = that.data.list[index].lat;
    var houseNumber = that.data.list[index].houseNumber;
    var data = {
      realName: name,
      tel: mobile,
      address: address,
      lat: lat,
      lng: lon,
      defaultAddress: isDefault,
      id: that.data.list[index].id,
      houseNumber: houseNumber
    }

    wx.showLoading({
      title: '',
      mask: true
    })
    utils.request('address/edit', data, function (res) {
      wx.hideLoading()
      if (res.data.code == 200) {
        that.getIndex()
      } else {
        utils.toast(res.data.msg)
      }
    })
  },
  delAddress(e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '确定要删除此地址吗？',
      success(res) {
        if (res.confirm) {
          wx.showLoading({
            title: '',
            mask: true
          })
          utils.request('address/del/' + id, {}, function (res) {
            wx.hideLoading()
            if (res.data.code == 200) {
              that.getIndex()
            } else {
              wx.hideLoading()
              utils.toast(res.data.msg)
            }
          })
        }
      }
    })
  },
  addAddressNew() {
    wx.navigateTo({
      url: '/pages/my/addAdress/addAdress',
    })
  },
  getIndex() {
    var that = this;
    utils.request('address/lists', { page: 1, limit: 100 }, function (res) {
      wx.stopPullDownRefresh();
      if (res.data.code == 200) {
        that.setData({
          list: res.data.data.rows
        })
      } else {
        wx.hideLoading()
        utils.toast(res.data.msg)
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  onPullDownRefresh() {
    this.getIndex();
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    if (that.data.firstOpen == 1) {
      this.getIndex()
    } else {
      that.setData({
        firstOpen: 1
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})